<?php
/**
 * User: hjd-yao
 * Date: 2018/11/4
 * Time: 17:18
 */

namespace App\Helpers;



class SelfResponse
{

    public static function make()
    {
        return new static();
    }

//    public static function success($msg,$route_name='',$data = [])
//    {
//        $url = self::refererUrl($route_name);
//        if(request()->ajax()){
//            return response()->json(self::responseSuccess($data,$url,$msg));
//        }else{
//            return redirect($url)->with('success',$msg);
//        }
//    }
//
//    public static function error($msg,$route_name='',$data=[])
//    {
//
//        $url = self::refererUrl($route_name);
//        if(request()->ajax()){
//            return response()->json(self::responseError($msg,0,$url));
//        }else{
//            return redirect($url)->with('errors',$msg)->withInput($data);
//        }
//    }


    public static function success($msg,$route_name='',$data = [])
    {
        return self::make()->jump($msg,1,$route_name,['data'=>$data]);
    }

    public static function error($msg,$route_name='',$data=[])
    {
        return self::make()->jump($msg,0,$route_name,['data'=>$data]);
    }

    public static function apiSuccess($msg,$data=[])
    {
        //return self::make()->jump($msg,200,'',['data'=>$data,'ajax'=>true]);
        return response()->json([
            'error_code'=>0,
            'msg'=>$msg,
            'data'=>$data
        ]);
    }

    public static function apiError($msg,$error_code=10000, $data=[])
    {
        //return self::make()->jump($msg,404,'',['data'=>$data,'ajax'=>true]);
        return response()->json([
            'error_code'=>$error_code,
            'msg'=>$msg,
            'data'=>$data
        ]);
    }

    public function jump($message, $status = 1, $routeName = '',$param = [])
    {
        if(isset($param['data'])&&!empty($param['data'])){
            $data['data'] = $param['data'];
        }
        $url = self::refererUrl($routeName);
        $data['url'] = $url;
        $data['msg'] = $message;
        $data['status'] = $status;

        if(request()->ajax() || (isset($param['ajax'])&&$param['ajax']== true)) {// AJAX提交
            return response()->json($data);
        }

        $data['waitSecond'] = $param['waitSecond']?? 5;
        $data['msgTitle'] =  $status ? trans('errors.operation_success') : trans('errors.operation_fail');
        if($status==1){
            $data['color'] = '#43AEFA';
        }else{
            $data['color'] = '#fc0a05';
        }
        return view('errors.jump',compact('data'));
    }

    public static function responseError($msg,$state_code=0,$url='')
    {
        return [
            'status'=>0,
            'code'=>$state_code,
            'url'=>$url,
            'msg' =>$msg
        ];
    }

    public static function responseSuccess($data=[],$url='',$msg='')
    {
        return [
            'status'=>1,
            'url'=>$url,
            'msg'=>$msg,
            'data'=>$data
        ];
    }

    /**
     * @title 跳转路由
     * @param $route_name
     * @return array|null|string
     */
    private static function  refererUrl($route_name)
    {
        if(is_array($route_name)){
            $url = empty($route_name)?request()->header('referer'):route($route_name[0],$route_name[1]);
        }else{
            $url = empty($route_name)?request()->header('referer'):route($route_name);
        }
        return $url;
    }


}